package initialize

import (
	"fmt"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"kuangstudy-mall/srvs/goods-srv/global"
	"kuangstudy-mall/srvs/goods-srv/handler"
	"kuangstudy-mall/srvs/goods-srv/proto"
	"net"
	"os"
	"os/signal"
	"syscall"
)

func InitGrpcServer() {
	port := global.Args.Port
	ip := global.Args.IP

	zap.S().Infof("服务开始启动，端口是：%d，IP是：%s", *port, *ip)
	// 启动gprc服务
	server := grpc.NewServer()
	// 开始运行用户服务的注册
	proto.RegisterGoodsServer(server, &handler.GoodsServiceHandler{})
	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *ip, *port))
	if err != nil {
		panic("failed to listen : " + err.Error())
	}

	go func() {
		err = server.Serve(listener)
		if err != nil {
			panic("failed to start gprc : " + err.Error())
		}
	}()

	zap.S().Info("服务启动成功，端口是：%d ", *global.Args.Port)
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	zap.S().Info("注销成功")
}
